package com.ophiux.cs.application.user.impl.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.core.Throws.ParamException;
import com.ophiux.cs.application.user.dto.updateUserInfoRequest;
import com.ophiux.cs.application.user.dto.updateUserInfoResponse;
import com.ophiux.cs.domain.user.model.entity.User;
import com.ophiux.cs.domain.user.repositories.IUserRepository;

import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class updateUserInfoService {
	@Autowired
	IUserRepository iUserRepository;
	
	public Result<updateUserInfoResponse> updateUserInfo(updateUserInfoRequest dto){
		Result<updateUserInfoResponse> result = new Result<updateUserInfoResponse>();
		updateUserInfoResponse res = new updateUserInfoResponse();
		try {
			try {
				//验证参数是否允许为空
				dto.paramVer();
			} catch (ParamException e) {
				return Result.fail(ResultEnum.requestParamFail.getCode(), e.getMessage()); 
			}
			
			User user = new User();
			user.setUSER_ID(dto.getUSER_ID());
			user.setUSER_NAME(dto.getUSER_NAME());
			user.setEMAIL(dto.getEMAIL());
			user.setSEX(dto.getSEX());
			user.setBIRTH_DAY(dto.getBIRTH_DAY());
			user.setADDRESS(dto.getADDRESS());
			
			boolean upResult = iUserRepository.upUserByUserId(user);
			if(upResult) {//修改成功
				res.setERROR_CODE(ResultEnum.requestSuccess.getCode());
				res.setERROR_MSG(ResultEnum.requestSuccess.getMsg());
				
				result = Result.success(res);
			}else {//未知失败 返回服务异常
				result = Result.fail(ResultEnum.requestException.getCode(), ResultEnum.requestException.getMsg());
			}
		} catch (Exception e) {
			log.info("updateUserRecoreInfo接口异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException.getCode(), ResultEnum.requestException.getMsg());
		}
		
		return result;
	} 
	 
}
 